package org.dandelion.core.remote
{
	import flash.utils.Dictionary;
	
	import mx.core.Application;
	
	import org.dandelion.cover.CoverManager;
	import org.dandelion.erro.DandelionError;
	import org.dandelion.erro.DandelionErrorInfo;
	
	public class RemoteService implements IRemoteService
	{
		private static var _instance:IRemoteService;
		private var _danelionRemoteObject:Dictionary = new Dictionary();
		public function RemoteService()
		{
			if(_instance) throw new Error("Error: Singletons can only be instantiated via instance() method!");
			RemoteService._instance = this;
		}
		public static function getInstance():IRemoteService 
		{
			if (_instance == null)
				_instance = new RemoteService();
			return _instance;
		}
		
		/**
		 * @param service
		 * 格式规则如下：
		 *	//－－－－－java写法－－－－－－－
		 *	//格式：destenation
		 *	//－－－－－.net写法－－－－－－
		 *	//格式：source|destenation.method
		 * 获取可用的远程服务对象
		 * @return IDandelionRemoteObject
		 */
		public function getDandelionRemoteObject(service:String):IDandelionRemoteObject
		{
			//格式：source|destenation.method
			var source:String = service.substring(0, service.indexOf("|"));
			if (source == null || source == "") source = service;
			else service = service.substring(service.indexOf("|") + 1, service.length);
			
			
			if (_danelionRemoteObject[source] == null)
				_danelionRemoteObject[source] = DandelionRemoteObjectFactory.getInstance().createDandelionRemoteObject(service, source);
			return _danelionRemoteObject[source];
		}
		
		
		
		
		/**
		 * 
		 * 通过此方法进行远程调用,显示遮罩效果
		 * 
		 * @param methodName
		  * 方法名称定义规则如下：
		 *	//－－－－－java写法－－－－－－－
		 *	//格式：destenation.method
		 *  //服务端方法格式：public User userLogin(User user)
		 *	public static const userLogin:String = "UserService.userLogin";
		 *	
		 *	//－－－－－.net写法－－－－－－
		 *	//格式：source|destenation.method
		 *	//服务端方法格式：public ArrayCollection GetPrintData(PrintData printData)
		 *	public static const GetPrintData:String = "serviceLibrary.DataService|fluorine.GetPrintData";
		 * 
		 * @param callback 调用成果的返回处理函数
		 * @param ...args 远程操作参数
		 * */
		public function coverCall(methodName:String, callback:Function, ...args):void
		{
			CoverManager.instance.cover(true);
			var idx:int = methodName.indexOf(".");
			if (idx == -1) 
			{
				throw new DandelionError(DandelionErrorInfo.METHODNMAE_FORMAT_NOT_CORRECT);
			}
			var method:String = methodName.substring(idx +1, methodName.length);
			var rs:IDandelionRemoteObject = getDandelionRemoteObject(methodName.substring(0,idx));
			rs.call(method,callback,args);
		}
		
		/**
		 * 
		 * 通过此方法进行远程调用,不显示遮罩效果
		 * 
		 * @param methodName
		 * 方法名称定义规则如下：
		 *	//－－－－－java写法－－－－－－－
		 *	//格式：destenation.method
		 *  //服务端方法格式：public User userLogin(User user)
		 *	public static const userLogin:String = "UserService.userLogin";
		 *	
		 *	//－－－－－.net写法－－－－－－
		 *	//格式：source|destenation.method
		 *	//服务端方法格式：public ArrayCollection GetPrintData(PrintData printData)
		 *	public static const GetPrintData:String = "serviceLibrary.DataService|fluorine.GetPrintData";
		 * 
		 * @param callback 调用成果的返回处理函数
		 * @param ...args 远程操作参数
		 * */
		public function call(methodName:String, callback:Function, ...args):void
		{
			var idx:int = methodName.indexOf(".");
			if (idx == -1) 
			{
				throw new DandelionError(DandelionErrorInfo.METHODNMAE_FORMAT_NOT_CORRECT);
			}
			var method:String = methodName.substring(idx +1, methodName.length);
			var rs:IDandelionRemoteObject = getDandelionRemoteObject(methodName.substring(0,idx));
			rs.call(method,callback,args);
		}
	}
}